package com.hotel.ordersystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.ResultUtils;
import com.common.utils.ResultVo;
import com.hotel.customersystem.pojo.CustomerUser;
import com.hotel.customersystem.service.CustomerUserService;
import com.hotel.ordersystem.controller.QiniuController;
import com.hotel.ordersystem.mapper.CommentListMapper;
import com.hotel.ordersystem.pojo.CommentList;
import com.hotel.ordersystem.pojo.CommentMongoDB;
import com.hotel.ordersystem.pojo.JsonVo;
import com.hotel.ordersystem.service.CommentListService;
import com.hotel.utils.Base64ToImgUtil;
import com.hotel.utils.UrlToMultipartFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * (CommentList)表服务实现类
 *
 * @author makejava
 * @since 2022-09-30 19:21:03
 */
@Service("commentListService")
public class CommentListServiceImpl extends ServiceImpl<CommentListMapper, CommentList> implements CommentListService {

    @Autowired
    private QiniuController qiniuController;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private CustomerUserService customerUserService;

    /**
     * 将图片存到七牛云并将七牛云返回的路径以及评论文本内容存储到mongoDB
     * @param jsonVo
     * @return
     */
    @Override
    public ResultVo<CommentMongoDB> saveQiniu(JsonVo jsonVo) {
        System.out.println(jsonVo);
        //该集合用于存放七牛云返回的url路径
        List<String> imagesList = new ArrayList<>();
        for (int i = 0; i < jsonVo.getImages().size(); i++) {
            String url = jsonVo.getImages().get(i);
            //Base64编码转换成图片形式
            //图片存在执行
            if (url != null){
                //图片存在的情况
                FileInputStream png = null;
                try {
                    png = Base64ToImgUtil.Base64ToImg(url);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String pngName = UUID.randomUUID().toString();
                File file = null;
                try {
                    file = UrlToMultipartFile.inputStreamToFile(png, pngName);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                MultipartFile multipartFile = UrlToMultipartFile.fileToMultipartFile(file);
                Map upload = qiniuController.upload(multipartFile);
                //目前获取imgUrl="路径"
                String imgUrlStr = (String) upload.get("imgUrl");
                String imgUrl = imgUrlStr.split("=")[0];
                imagesList.add(imgUrl);
            }else {
                //图片不存在,只存在文本
            }
        }
        //将评论内容封装成mongoDB所需要的对象
        CommentMongoDB comment = new CommentMongoDB();
        //mongoDB评论id
        String mangoDBCommentId = UUID.randomUUID().toString();
        comment.setId(mangoDBCommentId);
        //mongoDB评论内容
        comment.setText(jsonVo.getText());
        //mongoDB评论图片
        comment.setImages(imagesList);
        //mongoDB评论时间
        comment.setCommentTime(LocalDateTime.now());
        //mongoDB评论酒店id
        comment.setHotelId(jsonVo.getHotelId());
        //mongoDB评论订单id
        comment.setOrderId(jsonVo.getOrderId());
        //mongoDB评论是否显示(默认显示)
        comment.setIsVisible(1);
        //mongoDB评论评分
        comment.setScore(jsonVo.getScore());
        //mongoDB评论根据传过来的用户id查询用户名(未完成)
        QueryWrapper<CustomerUser> qw = new QueryWrapper<>();
        qw.eq("user_id",jsonVo.getUserId());
        CustomerUser customerUser = customerUserService.getOne(qw);
        System.out.println(customerUser);
        comment.setUserName(customerUser.getUserNickName());
        comment.setUserId(customerUser.getUserId());
        comment.setUserPhoto(customerUser.getStandby02());
        System.out.println("获取mongoDB评论对象");
        System.out.println(comment);
        mongoTemplate.insert(comment);
        return ResultUtils.success("您的评论已提交");
    }

}

